<?php
/** 
 * 
 *
 * @package api
 * @subpackage lang
 * @author Richard Fullmer <php-api@byu.edu>
 * @version 1.0
 */
/**
 *
 */
require_once( "lang/Number.php" );
require_once( "lang/NumberFormatException.php" );
require_once( "lang/Comparable.php" );

/**
 * Class Float
 *
 * The Float class wraps a value of primitive type float in an object. An object of type Float contains a single field whose 
 * type is float.
 * 
 * In addition, this class provides several methods for converting a float to a String and a String to a float, as well as 
 * other constants and methods useful when dealing with a float. 
 *
 * @package api
 * @subpackage lang
 */
class Float extends Number implements Comparable {

	// ===================================== Private Members ==============================================
	
	private $value;
	
	// ===================================== Public Methods ===============================================
	
	/**
	 * Constructs a newly allocated Float object that represents the argument converted to type float.
	 *
	 * @throws NumberFormatException if the $value cannot be converted to a float.
	 */
	public function __construct( $value ) {
		if ( is_float( $value ) )
			$this->value = $value;
		else
			throw new NumberFormatException();
	}
	
	/**
	 * Compares this object against the specified object.
	 *
	 * @returns boolean true if the value of this object equals the specified one.
	 */
	public function equals( Object $o ) {
		if ($o instanceof Float) {
			return $this->floatValue() == $o->floatValue();
		}
		return false;
	}
	
	/**
	 * Compares two Float objects numerically. 
	 *
	 * @param Object $o the Float object to be compared.
	 * @returns int the value 0 if $o is numerically equal to this Float; a value less than 0 if this Float  is numerically less 
	 * 		than $o; and a value greater than 0 if this Float is numerically greater than $o.
	 */
	public function compareTo( Object $o ) {
		if (!$o instanceof Float)
			throw new ClassCastException();
		
		return ($this->floatValue() < $o->floatValue() ? -1 : ($this->value == $o->floatValue() ? 0 : 1 ));
	}
	
	/**
	 * Returns a hashcode for this Float object.
	 *
	 * @return int the hashcode for this Float object.
	 */
	public function hashCode() {
		return $this->floatValue();
	}
	
	/**
	 * Returns the value of this Float object as an int (by casting to type int).
	 *
	 * @returns int the float value represented by this object converted to type int.
	 */
	public function intValue() {
		return (int) $this->value;
	}
	
	/**
	 * Returns the value of this Float object as a boolean (by casting to type boolean).
	 *
	 * @returns boolean the float value represented by this object converted to type boolean.
	 */
	public function booleanValue() {
		return (boolean) $this->value;
	}
	
	/**
	 * Returns the value of this Float object as an float.
	 *
	 * @returns float the float value represented by this object.
	 */
	public function floatValue() {
		return $this->value;
	}
	
	/**
	 * Returns the value of this Float object as an string (by casting to type string).
	 *
	 * @returns string the float value represented by this object converted to type string.
	 */
	public function stringValue() {
		return (string) $this->value;
	}
	
	// ===================================== Static Methods =================================================
	
	/**
	 * Compares the two specified float values. The sign of the integer value returned is the same as that of the 
	 * integer that would be returned by the call:
	 *
	 * 		new Float(f1).compareTo(new Float(f2))
	 *
	 * @param float $f1 - the first float to compare.
	 * @param float $f2 - the second float to compare.
	 * @returns int the value 0 if f1 is numerically equal to f2; a value less than 0 if f1 is numerically less than 
	 * 		f2; and a value greater than 0  if f1 is numerically greater than f2.
	 */
	public static function compare( $f1, $f2 ) {
		return new Float($f1)->compareTo( new Float($f2));
	}
}

?>